/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/* Import our local packages */
import tests.classtest;

/**
 *
 * @author Jimmy
 */
public class TestServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");
            out.println("</head>");
            out.println("<body>");
            // create the driver for MySQL
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            // setup the database URL
            String url = "jdbc:mysql://localhost:3306/team1";

            // establish the databse connection
            Connection con = DriverManager.getConnection(url, "team1", "abstract");

            // create a statement
            Statement s = con.createStatement();

            s.execute("SELECT * FROM testtable LIMIT 10");

            // retrieve the result
            ResultSet r = s.getResultSet();

            // get the metadata
            ResultSetMetaData meta = r.getMetaData();

            // get the number of columns
            int cols = meta.getColumnCount();

            // retrieve all of the rows
            while( r.next() ) {

                // retrieve all field values
                for(int i=0; i<cols; i++) {
                    out.print( r.getObject(i+1) +  "\t");
                }
                out.println("<br>");

            }


            /* TODO output your page here */
            out.println("<h1>Servlet TestServlet at " + request.getContextPath () + "</h1><br>");
            out.println(classtest.GetCurrentDate().toString());
        }
        catch (Exception e)
        {
            out.println("Exception: " + e.toString());
        } finally {
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
